package com.xinabei.mybitas.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.springframework.util.CollectionUtils;

/**
 * 生成mybitas代码生成工具的xml
 * 
 * @ClassName: GeneratorXmlUtils
 * @Description:TODO
 * @author: 鲜备科技
 * @date: 2017年12月21日 下午3:25:30
 * 
 * @Copyright: 2017 www.xianbeitech.com Inc. All rights reserved.
 *
 */
public class GeneratorXmlUtils {
	public String formatTableName(String tablename) {
		StringBuffer finalname = new StringBuffer();

		String[] split = tablename.split("_");
		for (String string : split) {
			String str = string.substring(0, 1).toUpperCase() + string.substring(1);
			finalname.append(str);
		}

		return finalname.toString();
	}

	public void getAllTables(List<String> inludeTables, List<String> exludeTables, String table_schema) {
		String sql = "select table_name from information_schema.tables where table_schema='%s' and table_type='base table';";
		sql = String.format(sql, table_schema);
		DBHelper db1 = new DBHelper(sql);
		try {
			ResultSet ret = db1.pst.executeQuery();
			while (ret.next()) {
				String tablename = ret.getString(1);
				if (exludeTables.size() > 0 && exludeTables.contains(tablename)) {
					continue;
				}

				if (inludeTables.size() > 0) {
					if (inludeTables.contains(tablename)) {
						String xml = "<table schema=\"%s\" tableName=\"%s\" domainObjectName=\"%s\"></table>";
						String objName = formatTableName(tablename);
						String format = String.format(xml, table_schema, tablename, objName);
						System.out.println(format);
					}
				} else {
					String xml = "<table schema=\"%s\" tableName=\"%s\" domainObjectName=\"%s\"></table>";
					String objName = formatTableName(tablename);
					String format = String.format(xml, table_schema, tablename, objName);
					System.out.println(format);

				}

			}
			ret.close();
			db1.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		GeneratorXmlUtils utils = new GeneratorXmlUtils();
		List<String> exludeTables = Arrays.asList();
		List<String> inludeTables = Arrays.asList("so");
		utils.getAllTables(inludeTables, exludeTables, "xb_cloud_product");
	}
}

class DBHelper {
	public static final String url = "jdbc:mysql://192.168.1.247:3306/xb_cloud_product";
	public static final String name = "com.mysql.jdbc.Driver";
	public static final String user = "root";
	public static final String password = "xb@sh2017";

	public Connection conn = null;
	public PreparedStatement pst = null;

	public DBHelper(String sql) {
		try {
			Class.forName(name);// 指定连接类型
			conn = DriverManager.getConnection(url, user, password);// 获取连接
			pst = conn.prepareStatement(sql);// 准备执行语句
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void close() {
		try {
			this.conn.close();
			this.pst.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}